package com.lbwnb;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView mLvNews;
    MyAdapter adapter;
    final ArrayList<News> newsArrayList = new ArrayList<>();
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {
        mLvNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News myNews = newsArrayList.get(position);
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                intent.putExtra("myNews",myNews);
                startActivity(intent);
            }
        });
    }


    private ArrayList<News> initData() {
        //用Volly包获取Json数据
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        String jsonUrl = "http://v.juhe.cn/toutiao/index?type=top&key=64c6d82ad39f1216cf371b8981c979a4";
        JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET,
                jsonUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONObject jsonObject = response.getJSONObject("result");
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject object = jsonArray.getJSONObject(i);
                        String title = object.getString("title");
                        String author_name = object.getString("author_name");
                        String date = object.getString("date");
                        News mNews = new News();

                        String thumbnail_pic_s = object.getString("thumbnail_pic_s");
                        mNews.setThumbnail_pic_s(thumbnail_pic_s);

//                        String thumbnail_pic_s02 = object.getString("thumbnail_pic_s02");
//                        mNews.setThumbnail_pic_s02(thumbnail_pic_s02);
//
//                        String thumbnail_pic_s03 = object.getString("thumbnail_pic_s03");
//                        mNews.setThumbnail_pic_s03(thumbnail_pic_s03);

                        mNews.setTitle(title);
                        mNews.setAuthor_name(author_name);
                        mNews.setDate(date);
                        newsArrayList.add(mNews);
                        //刷新适配器
                        adapter.notifyDataSetChanged();

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "出错辣出错辣", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(objectRequest);
        return newsArrayList;
    }

    private void initView() {
        mLvNews = findViewById(R.id.my_list);
        //绑定适配器==
        adapter = new MyAdapter();
        mLvNews.setAdapter(adapter);

    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return newsArrayList.size();
        }

        @Override
        public Object getItem(int i) {
            return newsArrayList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (view == null) {
                view = View.inflate(MainActivity.this, R.layout.list_item, null);

                TextView title = view.findViewById(R.id.title);
                TextView author_name = view.findViewById(R.id.category);
                TextView date = view.findViewById(R.id.date);
                ImageView image1 = view.findViewById(R.id.picture);
                //存储至viewHolder
                viewHolder = new ViewHolder();
                viewHolder.title = title;
                viewHolder.author_name = author_name;
                viewHolder.date = date;
                viewHolder.image1 = image1;
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.date.setText(newsArrayList.get(i).getDate());
            viewHolder.title.setText(newsArrayList.get(i).getTitle());
            viewHolder.author_name.setText(newsArrayList.get(i).getAuthor_name());
            Picasso.with(MainActivity.this)
                    .load(newsArrayList.get(i).getThumbnail_pic_s())
                    .into(viewHolder.image1);
            return view;
        }

        class ViewHolder {
            public TextView title;
            public TextView author_name;
            public TextView date;
            public ImageView image1;
        }
    }
}